package com.mentormobile.activies;

import com.mentormobile.R;
import com.mentormobile.model.IModelActivity;
import com.mentormobile.model.State;


import android.app.Activity;

import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;


public abstract class MasterActivity extends Activity implements IModelActivity{
	
	MenuItem save;
	MenuItem edit;
	MenuItem novo;
	MenuItem cancel;
	MenuItem delete;
	MenuItem search;
	private State state;
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		
		novo = menu.getItem(0);
		edit = menu.getItem(1);
		save = menu.getItem(2);
		search = menu.getItem(3);
		cancel = menu.getItem(4);
		delete = menu.getItem(5);
		save.setEnabled(false);
		edit.setEnabled(false);
		novo.setEnabled(true);
		cancel.setEnabled(false);
		delete.setEnabled(false);
		search.setEnabled(true);
		Log.d("Criou", "Criou o menu");
		return true;
		
		//super.onCreateOptionsMenu(menu);
		//return true ;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.cancel:
			cancelMaster();
			return true;
		case R.id.delete:
			deleteMaster();
			return true;
		case R.id.save:
			saveMaster();
			return true;
		case R.id.search:
			searchMaster();
			return true;
		case R.id.edit:
			editMaster();
			return true;
		case R.id.novo:
			novoMaster();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	public void novoMaster() {
		
		setState(State.INSERT);
		novo();
		
		
	}
	public void editMaster() {
		setState(State.EDIT);
		edit();
		
	}
	public void saveMaster() {
		setState(State.SAVE);
		save();
		
	}
	public void searchMaster() {
		setState(State.BROWSE);
		search();
		
	}
	public void deleteMaster() {
		setState(State.DELETE);
		delete();
		
	}
	public void cancelMaster() {
		setState(State.CANCEL);
		cancel();
		
	}
	public State getState() {
		return state;
	}
	public void setState(State state) {
		this.state = state;
		
		switch (this.state) {//bolar um jeito melhor de fazer esse switch
		case BROWSE:
			save.setEnabled(false);
			edit.setEnabled(true);
			novo.setEnabled(true);
			cancel.setEnabled(false);
			delete.setEnabled(true);
			search.setEnabled(true);
			return;
		case INSERT:
			save.setEnabled(true);
			edit.setEnabled(false);
			novo.setEnabled(false);
			cancel.setEnabled(true);
			delete.setEnabled(false);
			search.setEnabled(false);
			return;
		case CANCEL:
			save.setEnabled(false);
			edit.setEnabled(false);
			novo.setEnabled(true);
			cancel.setEnabled(false);
			delete.setEnabled(false);
			search.setEnabled(true);
			return;
		case DELETE:
			save.setEnabled(false);
			edit.setEnabled(false);
			novo.setEnabled(true);
			cancel.setEnabled(false);
			delete.setEnabled(false);
			search.setEnabled(true);
			return;
		case SAVE:
			save.setEnabled(false);
			edit.setEnabled(true);
			novo.setEnabled(true);
			cancel.setEnabled(false);
			delete.setEnabled(true);
			search.setEnabled(true);
			return;
		case EDIT:
			save.setEnabled(true);
			edit.setEnabled(false);
			novo.setEnabled(false);
			cancel.setEnabled(true);
			delete.setEnabled(false);
			search.setEnabled(false);
			return;
		default:
			return;
		}
		
	}

	
	
}
